﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;

namespace Draco.OWF.Studio.Common.Geometry
{
    /// <summary>
    /// Relationship between Point and Rectangle
    /// </summary>
    public enum PointRectRelation
    {
        Inside, On, Outside
    }
    /// <summary>
    /// Relationship between LineSeg and Rectangle
    /// </summary>
    public enum LineSegRectRelation
    {
        Inside, On, Intersection, Outside
    }
    public class GeoRect
    {
        public Rect Rect { get; private set; }
        public GeoRect(Rect rect)
        {
            this.Rect = rect;
        }
        public GeoRect(Point leftTop,double width,double height)
        {
            this.Rect = new Rect(leftTop.X, leftTop.Y, width, height) ;
        }


        #region Point and Rect Relationship
        /// <summary>
        /// Get Relationship between point and rect
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        public PointRectRelation GetPointRectRelationship(Point p)
        {
            if (IsPointInRect(p)) return PointRectRelation.Inside;
            if (IsPointOnRect(p)) return PointRectRelation.On;
            return PointRectRelation.Outside;
        }
        /// <summary>
        /// Get true if Point is inside of Rectangle
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private bool IsPointInRect(Point p)
        {
            return p.X > this.Rect.Left && p.X < this.Rect.Right && p.Y > this.Rect.Top && p.Y < this.Rect.Bottom;
        }
        /// <summary>
        /// Get true if Point is on Rectangle
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private bool IsPointOnRect(Point p)
        {
            return (p.X == this.Rect.Left && p.Y >= this.Rect.Top && p.Y <= this.Rect.Bottom) ||    //left side
                (p.X == this.Rect.Right && p.Y >= this.Rect.Top && p.Y <= this.Rect.Bottom) ||      //right side
                (p.Y == this.Rect.Top && p.X >= this.Rect.Left && p.X <= this.Rect.Right) ||        //top side
                (p.Y == this.Rect.Bottom && p.X >= this.Rect.Left && p.X <= this.Rect.Right);      //buttom side
        }
        /// <summary>
        /// Get true if Point is outside of Rectangle
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private bool IsPointOutOfRect(Point p)
        {
            return !(IsPointInRect(p) || IsPointOnRect(p));
        }
        #endregion
    }
}
